//
//  MerchantDetailVC.swift
//  kxf
//
//  Created by Elavatine on 2024/10/16.
//

import MCToast

class MerchantDetailVC: WHBaseViewVC {
    
    var msgDict = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
    }
    
    lazy var mercMsgTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "商户信息"
        return vm
    }()
    lazy var mercNoItemVM: MercDetailItemVM = {
        let vm = MercDetailItemVM.init(frame: CGRect.init(x: 0, y: self.mercMsgTitleVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "平台商户号"
        vm.detailLabel.text = "\(self.msgDict["bangdingcommercode"]as? String ?? "无")"
        
        if self.msgDict["bangdingcommercode"]as? String ?? "" == ""{
            vm.copyButton.isHidden = true
        }
        
        vm.copyBlock = {()in
            self.copyAction(psString: self.msgDict["bangdingcommercode"]as? String ?? "", tips: "平台商户号")
        }
        
        return vm
    }()
    lazy var mercNameItemVM: MercDetailItemVM = {
        let vm = MercDetailItemVM.init(frame: CGRect.init(x: 0, y: self.mercNoItemVM.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "商户名称"
        vm.detailLabel.text = "\(self.msgDict["merchantName"]as? String ?? "无")"
        if self.msgDict["merchantName"]as? String ?? "" == ""{
            vm.copyButton.isHidden = true
        }
        vm.copyBlock = {()in
            self.copyAction(psString: self.msgDict["merchantName"]as? String ?? "", tips: "商户名称")
        }
        return vm
    }()
    lazy var mercShortNameItemVM: MercDetailItemVM = {
        let vm = MercDetailItemVM.init(frame: CGRect.init(x: 0, y: self.mercNameItemVM.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "商户简称"
        vm.detailLabel.text = "\(self.msgDict["shortName"]as? String ?? "无")"
        if self.msgDict["shortName"]as? String ?? "" == ""{
            vm.copyButton.isHidden = true
        }
        vm.copyBlock = {()in
            self.copyAction(psString: self.msgDict["shortName"]as? String ?? "", tips: "商户简称")
        }
        return vm
    }()
    
    lazy var mercChannelTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: self.mercShortNameItemVM.frame.maxY + kFitWidth(8), width: 0, height: 0))
        vm.titleLabel.text = "通道：\(self.msgDict["posChannelName"]as? String ?? "")"
        if msgDict["posStatus"]as? Int ?? -5 == 4{
            vm.arrowImgViewVer.isHidden = false
            vm.tapBlock = {()in
                self.sendGetMsgRequset(tel: self.msgDict["phone"]as? String ?? "")
            }
        }else{
            vm.isUserInteractionEnabled = false
        }
        
        return vm
    }()
    lazy var channelMercNoItemVM: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.mercChannelTitleVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "通道商户号：\(self.msgDict["mchId"]as? String ?? "")"
        if (self.msgDict["mchId"]as? String ?? "").count > 0 {
            vm.copyButton.isHidden = false
        }
        vm.copyBlock = {()in
            self.copyAction(psString: self.msgDict["mchId"]as? String ?? "", tips: "通道商户号")
        }
        return vm
    }()
    lazy var mchTypeItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.channelMercNoItemVM.frame.maxY, width: 0, height: 0))
        if self.msgDict["mchType"]as? String ?? "" == "1"{
            vm.leftTitleLabel.text = "商户类型：小微商户"
        }else if self.msgDict["mchType"]as? String ?? "" == "2"{
            vm.leftTitleLabel.text = "商户类型：个体商户"
        }else{
            vm.leftTitleLabel.text = "商户类型：企业商户"
        }
         
        return vm
    }()
    lazy var proNameItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.mchTypeItemVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "项目名称：\(self.msgDict["bankActivityName"]as? String ?? "")"
        return vm
    }()
    lazy var posStatusItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.proNameItemVm.frame.maxY, width: 0, height: 0))
//        vm.leftTitleLabel.text = "入网状态：\(self.msgDict["status"]as? String ?? "")"
        
        let statuAttr = NSMutableAttributedString.init(string: "入网状态：")
        let statuA = NSMutableAttributedString.init(string: "\(self.msgDict["status"]as? String ?? "")")
        statuA.yy_color = WHColor_16(colorStr: "F10E0E")
        statuAttr.append(statuA)
        
        vm.leftTitleLabel.attributedText = statuAttr
        
        return vm
    }()
    lazy var signStatusItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.posStatusItemVm.frame.maxY, width: 0, height: 0))
//        vm.leftTitleLabel.text = "签约状态：\((self.msgDict["signStatus"]as? String ?? "") == "1" ? "已签约" : "未签约")"
        
        let statuAttr = NSMutableAttributedString.init(string: "签约状态：")
        let statuA = NSMutableAttributedString.init(string: "\((self.msgDict["signStatus"]as? String ?? "") == "1" ? "已签约" : "未签约")")
        statuA.yy_color = WHColor_16(colorStr: "F10E0E")
        statuAttr.append(statuA)
        
        vm.leftTitleLabel.attributedText = statuAttr
        
        return vm
    }()
    lazy var settleWayItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.signStatusItemVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "结算方式：\(self.msgDict["settleCycleName"]as? String ?? "")"
        return vm
    }()
    lazy var wxMercNoItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.settleWayItemVm.frame.maxY, width: 0, height: 0))
        
        vm.tapBlock = {()in
            let vc = MerchantAuthoriVC()
            vc.phone = self.msgDict["phone"]as? String ?? ""
            self.navigationController?.pushViewController(vc, animated: true)
            vc.authoriBlock = {()in
                let dict = NSMutableDictionary(dictionary: self.msgDict)
                dict.setValue("AUTHORIZED", forKey: "wechatAuthorizeStatus")
                self.msgDict = dict
                self.updateAuthoriStatus()
            }
        }
        return vm
    }()
    lazy var alipayMercNoItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.wxMercNoItemVm.frame.maxY, width: 0, height: 0))
        
        vm.tapBlock = {()in
            let vc = MerchantAuthoriVC()
            vc.type = "alipay"
            vc.phone = self.msgDict["phone"]as? String ?? ""
            self.navigationController?.pushViewController(vc, animated: true)
            vc.authoriBlock = {()in
                let dict = NSMutableDictionary(dictionary: self.msgDict)
                dict.setValue("AUTHORIZED", forKey: "alipayAuthorizeStatus")
                self.msgDict = dict
                self.updateAuthoriStatus()
            }
        }
        
        return vm
    }()
    
    lazy var cTimeItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.alipayMercNoItemVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "入网时间：\(self.msgDict["createDate"]as? String ?? "")"
        return vm
    }()
    lazy var deviceNumberItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.cTimeItemVm.frame.maxY, width: 0, height: 0))
        
        vm.tapBlock = {()in
            DLLog(message: "管理设备列表")
            UserModel.shared.mchUserIdForDevice = self.msgDict["userId"]as? String ?? ""
            let vc = DeviceTypeListVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        return vm
    }()
    
    lazy var whiteItemVm: MercDetailChannelItemVM = {
        let vm = MercDetailChannelItemVM.init(frame: CGRect.init(x: 0, y: self.deviceNumberItemVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = ""
        return vm
    }()
}

extension MerchantDetailVC{
    func copyAction(psString:String,tips:String) {
        if psString == ""{
            MCToast.mc_text("无\(tips)")
            return
        }
        UIPasteboard.general.string = psString
        MCToast.mc_text("\(tips)已复制！")
    }
}

extension MerchantDetailVC{
    func initUI() {
        initNavi(titleStr: "商户信息")
        
        view.addSubview(scrollView)
        scrollView.frame = CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(8), width: SCREEN_WIDHT, height: SCREEN_HEIGHT - getNavigationBarHeight())
        
        scrollView.addSubview(mercMsgTitleVm)
        scrollView.addSubview(mercNoItemVM)
        scrollView.addSubview(mercNameItemVM)
        scrollView.addSubview(mercShortNameItemVM)
        scrollView.addSubview(mercChannelTitleVm)
        scrollView.addSubview(channelMercNoItemVM)
        scrollView.addSubview(mchTypeItemVm)
        scrollView.addSubview(proNameItemVm)
        scrollView.addSubview(posStatusItemVm)
        scrollView.addSubview(signStatusItemVm)
        scrollView.addSubview(settleWayItemVm)
        scrollView.addSubview(wxMercNoItemVm)
        scrollView.addSubview(alipayMercNoItemVm)
        scrollView.addSubview(cTimeItemVm)
        scrollView.addSubview(deviceNumberItemVm)
        scrollView.addSubview(whiteItemVm)
        
        scrollView.contentSize = CGSize.init(width: 0, height: self.whiteItemVm.frame.maxY + kFitWidth(40))
        
        updateAuthoriStatus()
    }
    func updateAuthoriStatus() {
        let statuAttr = NSMutableAttributedString.init(string: "微信子商户号：\(self.msgDict["wechatCode"]as? String ?? "")")
        
        if self.msgDict["wechatAuthorizeStatus"]as? String ?? "" == "AUTHORIZED" {
            let statuA = NSMutableAttributedString.init(string: "|已授权")
            statuA.yy_color = WHColor_16(colorStr: "F10E0E")
            statuAttr.append(statuA)
            wxMercNoItemVm.isUserInteractionEnabled = false
        }else{
            let statuA = NSMutableAttributedString.init(string: "|未授权")
            statuA.yy_color = WHColor_16(colorStr: "999999")
            statuAttr.append(statuA)
        }
        
        wxMercNoItemVm.leftTitleLabel.attributedText = statuAttr
        
        
        let statuAliAttr = NSMutableAttributedString.init(string: "支付宝子商户号：\(self.msgDict["aliCode"]as? String ?? "")")
        
        if self.msgDict["alipayAuthorizeStatus"]as? String ?? "" == "AUTHORIZED" {
            let statuA = NSMutableAttributedString.init(string: "|已授权")
            statuA.yy_color = WHColor_16(colorStr: "F10E0E")
            statuAliAttr.append(statuA)
            alipayMercNoItemVm.isUserInteractionEnabled = false
        }else{
            let statuA = NSMutableAttributedString.init(string: "|未授权")
            statuA.yy_color = WHColor_16(colorStr: "999999")
            statuAliAttr.append(statuA)
        }
        
        alipayMercNoItemVm.leftTitleLabel.attributedText = statuAliAttr
        
        let deviceNumAttr = NSMutableAttributedString.init(string: "设备数量：")
        let numA = NSMutableAttributedString.init(string: "\(self.msgDict["devicesNumber"]as? String ?? "0")")
        numA.yy_color = WHColor_16(colorStr: "F10E0E")
        deviceNumAttr.append(numA)
        deviceNumberItemVm.leftTitleLabel.attributedText = deviceNumAttr
    }
}

extension MerchantDetailVC{
    func sendGetMsgRequset(tel:String) {
        MCToast.mc_loading()
        let urlString = "\(URL_authen_msg_query)?tel=\(tel)"
        
        WHNetworkUtil.shareManager().GET(urlString: urlString,vc:self) { responseObject in
            DLLog(message: "\(responseObject)")
            
            let dataObj = responseObject["data"]as? NSDictionary ?? [:]
            UserModel.shared.posChannel = dataObj["posChannel"]as? String ?? "ys"
            if dataObj["merchantType"]as? String ?? "" == "0" {
                AuthenCompanyMsgModel.shareInstance.refreshMsg(dict: responseObject["data"]as? NSDictionary ?? [:])
                
                if AuthenCompanyMsgModel.shareInstance.posChannel == "fy"{
                    let vc = AuthenCompanyFYVC()
                    vc.isUpdate = true
                    vc.isRead = true
                    self.navigationController?.pushViewController(vc, animated: true)
                }else{
                    let vc = AuthenCompanyVC()
                    vc.isUpdate = true
                    vc.isRead = true
                    self.navigationController?.pushViewController(vc, animated: true)
                }
            }else if dataObj["merchantType"]as? String ?? "" == "1" {
                AuthenMiniMsgModel.shareInstance.refreshMsg(dict: responseObject["data"]as? NSDictionary ?? [:])
                let vc = AuthenMiniVC()
                vc.isUpdate = true
                vc.isRead = true
                self.navigationController?.pushViewController(vc, animated: true)
            }else if dataObj["merchantType"]as? String ?? "" == "2" {
                AuthenCompanyMsgModel.shareInstance.refreshMsg(dict: responseObject["data"]as? NSDictionary ?? [:])
                let vc = AuthenGeTiVC()
                vc.isUpdate = true
                vc.isRead = true
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}
